/*
 * Copyright (C) 2020 Baidu, Inc. All Rights Reserved.
 */
package optional;

import com.sun.xml.internal.messaging.saaj.packaging.mime.util.LineInputStream;

import javax.swing.*;
import java.awt.event.ItemEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
 * @Author wangtengyu
 * @Date 2020/7/2 5:05 下午
 * @Description
 */
public class Test1 {

    public static void main(String args[]){
//        testNull();
//        testIsPresent();
//        testNullAble();
//        testEmpty();
//        testEmptyAndGet();
//        testOrElse();
//        testMap();
          testFlatMap();
          testFilter();

    }


    private static void  testEmpty(){
        //测试下empty和get,抛异常
        System.out.println(Optional.empty());
    }

    private static void testNull(){
        //报错
        Optional.of(null).ifPresent(System.out::print);
    }

    private static void testNullAble(){
        Optional.ofNullable(null).ifPresent(System.out::print);
        Optional.ofNullable("123").ifPresent(System.out::print);
    }

    private static void testIsPresent(){
        System.out.println(Optional.empty().isPresent());
        System.out.println(Optional.of("123").isPresent());
    }

    private static void  testEmptyAndGet(){
        //测试下empty和get,抛异常
        System.out.println(Optional.empty().get());
    }

    private static void testOrElse(){
        System.out.println(Optional.ofNullable(null).orElse("123"));
        System.out.println(Optional.ofNullable(null).orElseGet(()->"10086"));
        Optional.ofNullable(null).orElseThrow(()->new RuntimeException("testexception"));
    }


    private static void testMap(){
        System.out.println(Optional.of("ABC").map(value->value.toLowerCase()).get());
        System.out.println(Optional.ofNullable(null).map((value)->"123"));
    }


    private static void testFlatMap(){
        System.out.println(Optional.of("ABC").map(value->Optional.of(value)).get());
        System.out.println(Optional.of("ABC").flatMap(value->Optional.of(value)).get());
    }


    private static void testFilter(){
        List list= new ArrayList();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.stream().forEach(item->System.out.println(Optional.of(item).filter(value->(int)value>4).orElse("zzz")));

    }







}
